package cn.anecansaitin.inet.mixins;

import cn.anecansaitin.inet.TranslateHelper;
import mcjty.lib.container.GenericContainer;
import mcjty.lib.gui.GenericGuiContainer;
import mcjty.lib.gui.ManualEntry;
import mcjty.lib.gui.Window;
import mcjty.lib.gui.widgets.*;
import mcjty.lib.varia.BlockPosTools;
import mcjty.rftoolsbase.api.xnet.channels.IChannelType;
import mcjty.xnet.XNet;
import mcjty.xnet.client.ControllerChannelClientInfo;
import mcjty.xnet.modules.router.blocks.TileEntityRouter;
import mcjty.xnet.modules.router.client.GuiRouter;
import mcjty.xnet.setup.XNetMessages;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;

@Mixin(GuiRouter.class)
public abstract class RouterGuiMixin extends GenericGuiContainer<TileEntityRouter, GenericContainer> {
    public RouterGuiMixin(TileEntityRouter tileEntity, GenericContainer container, Inventory inventory, ManualEntry manualEntry) {
        super(tileEntity, container, inventory, manualEntry);
    }

    @Final
    @Shadow
    private static ResourceLocation iconGuiElements;
    @Shadow
    private WidgetList localChannelList;
    @Shadow
    private WidgetList remoteChannelList;
    @Shadow
    private int listDirty;

    @Shadow
    protected abstract void updatePublish(BlockPos pos, int index, String name);
    @Shadow
    protected abstract void refresh();

    @Override
    public void init() {
        window = new Window(this, tileEntity, XNetMessages.INSTANCE, new ResourceLocation(XNet.MODID, "gui/router.gui"));
        Panel toplevel = (Panel) window.getToplevel();
        List<Widget<?>> children = toplevel.getChildren();
        ((Label) children.get(2)).text(TranslateHelper.getText("inet.router.label.local"));
        ((Label) children.get(3)).text(TranslateHelper.getText("inet.router.label.remote"));
        super.init();

        localChannelList = window.findChild("localchannels");
        remoteChannelList = window.findChild("remotechannels");

        refresh();
        listDirty = 0;
    }

    @Inject(method = "makeChannelLine", at = @At(value = "HEAD"), remap = false, cancellable = true)
    private void mixin$makeChannelLine(ControllerChannelClientInfo channel, boolean local, CallbackInfoReturnable<Panel> cir){
        String name = channel.getChannelName();
        String publishedName = channel.getPublishedName();
        BlockPos controllerPos = channel.getPos();
        IChannelType type = channel.getChannelType();
        int index = channel.getIndex();

        Panel panel = Widgets.positional().desiredHeight(30);
        Panel panel1 = Widgets.horizontal(0, 0).hint(0, 0, 160, 13);
        int labelColor = 0xff2244aa;
        // @todo, better way to show remote channels
        if (channel.isRemote()) {
            labelColor = 0xffaa1133;
        }
        panel1.children(
                Widgets.label(TranslateHelper.getText("inet.router.channel_list.label.channel")).color(labelColor),
                Widgets.label(name),
                Widgets.label(">").color(labelColor));
        if (channel.isRemote()) {
            panel1.children(new ImageLabel().image(iconGuiElements, 48, 80).desiredWidth(16));
        }
        if (local) {
            TextField pubName = new TextField().text(publishedName).desiredWidth(50).desiredHeight(13)
                    .event((newText) -> updatePublish(controllerPos, index, newText));
            panel1.children(pubName);
        } else {
            panel1.children(Widgets.label(publishedName).color(0xff33ff00));
        }

        Panel panel2 = Widgets.horizontal(0, 0).hint(0, 13, 160, 13)
                .children(
                        Widgets.label(TranslateHelper.getText("inet.router.channel_list.label.pos")).color(labelColor),
                        Widgets.label(BlockPosTools.toString(controllerPos)));

        Panel panel3 = Widgets.horizontal(0, 0).hint(0, 26, 160, 13)
                .children(
                        Widgets.label(TranslateHelper.getText("inet.router.channel_list.label.index")).color(labelColor),
                        Widgets.label(index + " (" + type.getName() + ")"));

        panel.children(panel1, panel2, panel3);
        cir.setReturnValue(panel);
        cir.cancel();
    }
}
